﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace AF_Web
{
    public static class AspxGetter
    {
        public static bool TryGetRequestString(this Page context, string key, out string Value)
        {
            Value = "";
            bool reuslt = false;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                Value = context.Server.UrlDecode(str);
                reuslt = true;
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                Value = context.Server.UrlDecode(str);
                reuslt = true;
            }
            return reuslt;
        }

        public static bool TryGetRequestInt(this Page context, string key, out int Value)
        {
            Value = 0;
            bool result = false;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                result = int.TryParse(str, out Value);
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                result = int.TryParse(str, out Value);
            }
            return result;
        }

        public static bool TryGetRequestFloat(this Page context, string key, out float Value)
        {
            Value = 0;
            bool result = false;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                result = float.TryParse(str, out Value);
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                result = float.TryParse(str, out Value);
            }
            return result;
        }

        public static bool TryGetRequestGuid(this Page context, string key, out Guid Value)
        {
            Value = Guid.Empty;
            bool result = false;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                result = Guid.TryParse(str, out Value);
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                result = Guid.TryParse(str, out Value);
            }
            return result;
        }

        public static bool TryGetRequestDateTime(this Page context, string key, out DateTime Value)
        {
            Value = DateTime.MinValue;
            bool result = false;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                result = DateTime.TryParse(str, out Value);
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                result = DateTime.TryParse(str, out Value);
            }
            return result;
        }

        public static bool TryGetRequestDecimal(this Page context, string key, out decimal Value)
        {
            Value = 0;
            bool result = false;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                result = decimal.TryParse(str, out Value);
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                result = decimal.TryParse(str, out Value);
            }
            return result;
        }
        public static bool TryGetRequestBoolean(this Page context, string key, out bool Value)
        {
            Value = false;
            bool result = false;
            if (context.Request.QueryString[key] != null)
            {
                string str = context.Request.QueryString[key];
                result = bool.TryParse(str, out Value);
            }
            else if (context.Request.Form[key] != null)
            {
                string str = context.Request.Form[key];
                result = bool.TryParse(str, out Value);
            }
            return result;
        }
    }
}